package org.zcode.generator.core.generator.clazz;

import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.zcode.generator.core.config.BeanTypeConfig;
import org.zcode.generator.core.config.ImportConfig;
import org.zcode.generator.core.config.XmlConfig;
import org.zcode.generator.core.generator.ClazzCreator;
import org.zcode.generator.core.generator.MethodCreator;
import org.zcode.generator.core.generator.method.DAOTestMethodCreator;
import org.zcode.generator.core.util.GeneratorUtil;

/**
 * @Description:
 * @author zhoulk
 * @date: 2019/10/31
 */
public class DAOTestClazzCreator extends ClazzCreator {
    private MethodCreator methodFactory;
    private String tableName;
    private String tableRemarks;
    private TopLevelClass serviceImpl;

    public DAOTestClazzCreator(XmlConfig xmlConfig, BeanTypeConfig beanTypeConfig, IntrospectedTable introspectedTable) {
        super(xmlConfig, beanTypeConfig, introspectedTable);
        this.tableRemarks = GeneratorUtil.getTableRemarks(introspectedTable.getRemarks());
        String table = introspectedTable.getBaseRecordType();
        this.tableName = table.substring(table.lastIndexOf(".") + 1).substring(xmlConfig.substring);
        methodFactory = new DAOTestMethodCreator(xmlConfig, beanTypeConfig, introspectedTable);
        this.init();
        this.addImport();
    }

    @Override
    protected void init() {
        FullyQualifiedJavaType serviceType = new FullyQualifiedJavaType(xmlConfig.servicePack.replace("service","dao") + "." + tableName + "MapperTest");
        serviceImpl = new TopLevelClass(serviceType);
        serviceImpl.addAnnotation("@SpringBootTest(classes = " + xmlConfig.applicationName + ".class)");
        serviceImpl.addAnnotation("@RunWith(SpringJUnit4ClassRunner.class)");
    }

    @Override
    protected GeneratedJavaFile createInterface() {
        return null;
    }

    /**
     * 添加实现类
     */
    @Override
    public GeneratedJavaFile createClazz() {
        serviceImpl.setVisibility(JavaVisibility.PUBLIC);
        this.addField(serviceImpl);
        // 添加方法
        Method method = methodFactory.countMethod(tableName, 0);
        serviceImpl.addMethod(method);

        method = methodFactory.qrysMethod(tableName, 0);
        serviceImpl.addMethod(method);
        if (introspectedTable.getPrimaryKeyColumns() != null && introspectedTable.getPrimaryKeyColumns().size() > 0) {
            method = methodFactory.qryByIdMethod(tableName, 0);
            serviceImpl.addMethod(method);
        }
        method = methodFactory.addMethod(tableName, 0);
        serviceImpl.addMethod(method);

        method = methodFactory.updMethod(tableName, 0);
        serviceImpl.addMethod(method);
        // 生成文件
        return new GeneratedJavaFile(serviceImpl, xmlConfig.datasourceTestTarget);
    }

    @Override
    protected void addField(TopLevelClass serviceImpl) {
        // 添加 dao
        Field field = new Field();
//        field.setName("LOGGER = LoggerFactory.getLogger(" + serviceImpl.getType().getShortName() + ".class)"); // 设置变量名
//        field.setType(ImportConfig.Logger); // 类型
//        field.setStatic(true);
//        field.setFinal(true);
//        field.setVisibility(JavaVisibility.PRIVATE);
//        serviceImpl.addField(field);
        field = new Field();
        field.setName(GeneratorUtil.toLowerCase(beanTypeConfig.daoType.getShortName())); // 设置变量名
        serviceImpl.addImportedType(beanTypeConfig.daoType);
        field.setType(beanTypeConfig.daoType); // 类型
        field.setVisibility(JavaVisibility.PRIVATE);
        field.addAnnotation("@Autowired");
        serviceImpl.addField(field);
    }

    @Override
    protected void addImport() {
        serviceImpl.addJavaDocLine(GeneratorUtil.getDocTmpl(tableRemarks + "Mapper测试用例", xmlConfig.author));
        serviceImpl.addImportedType(ImportConfig.Test);
        serviceImpl.addImportedType(ImportConfig.RunWith);
//        serviceImpl.addImportedType(ImportConfig.Logger);
        serviceImpl.addImportedType(ImportConfig.SpringBootTest);
        serviceImpl.addImportedType(ImportConfig.SpringJUnit4ClassRunner);
        serviceImpl.addImportedType(ImportConfig.listType);
        serviceImpl.addImportedType(ImportConfig.DATE);
        serviceImpl.addImportedType(ImportConfig.autowired);
//        serviceImpl.addImportedType(ImportConfig.LoggerFactory);
        serviceImpl.addImportedType(beanTypeConfig.pojoType);
        serviceImpl.addImportedType(beanTypeConfig.pojoConditionType);
        serviceImpl.addImportedType(new FullyQualifiedJavaType(xmlConfig.applicationClazz));
    }
}
